default:
  interruptible: true

workflow:
  name: $PIPELINE_NAME

include:
  - project: gitlab-org/quality/pipeline-common
    ref: 7.2.3
    file:
      - /ci/base.gitlab-ci.yml
      - /ci/knapsack-report.yml

stages:
  - test
  - report
  - notify

# ==========================================
# Templates
# ==========================================
.parallel:
  parallel: 5
  variables:
    QA_KNAPSACK_REPORT_PATH: $CI_PROJECT_DIR/qa/knapsack

.ruby-image:
  # Because this pipeline template can be included directly in other projects,
  # image path and registry needs to be defined explicitly
  image: ${REGISTRY_HOST}/${REGISTRY_GROUP}/gitlab-build-images/debian-${DEBIAN_VERSION}-ruby-${RUBY_VERSION}:bundler-2.3

.bundler-variables:
  variables:
    BUNDLE_SUPPRESS_INSTALL_USING_MESSAGES: "true"
    BUNDLE_SILENCE_ROOT_WARNING: "true"

.qa-install:
  extends:
    - .bundler-variables
    - .gitlab-qa-install

.update-script:
  script:
    - !reference [.bundle-prefix]
    - export QA_COMMAND="$BUNDLE_PREFIX gitlab-qa Test::Omnibus::UpdateFromPrevious $RELEASE $GITLAB_SEMVER_VERSION $UPDATE_TYPE $UPDATE_FROM_EDITION -- $QA_RSPEC_TAGS $RSPEC_REPORT_OPTS"
    - echo "Running - '$QA_COMMAND'"
    - eval "$QA_COMMAND"

.qa:
  extends:
    - .bundler-variables
    - .qa-base
    - .gitlab-qa-report
  stage: test
  tags:
    - e2e
  variables:
    QA_GENERATE_ALLURE_REPORT: "true"
    QA_CAN_TEST_PRAEFECT: "false"
    QA_INTERCEPT_REQUESTS: "true"
    GITLAB_LICENSE_MODE: test
    GITLAB_QA_ADMIN_ACCESS_TOKEN: $QA_ADMIN_ACCESS_TOKEN
    GITLAB_QA_OPTS: $EXTRA_GITLAB_QA_OPTS
  before_script:
    - !reference [.qa-base, before_script]
    # Prepend the file paths with the absolute path from inside the container since the files will be read from there
    - export RSPEC_FAST_QUARANTINE_PATH="/home/gitlab/qa/${RSPEC_FAST_QUARANTINE_PATH}"
    - export RSPEC_SKIPPED_TESTS_REPORT_PATH="/home/gitlab/qa/rspec/skipped_tests-${CI_JOB_ID}.txt"
  # Allow QA jobs to fail as they are flaky. The top level `package-and-e2e:ee`
  # pipeline is not allowed to fail, so without allowing QA to fail, we will be
  # blocking merges due to flaky tests.
  allow_failure: true

.download-knapsack-report:
  extends:
    - .gitlab-qa-image
  stage: .pre
  variables:
    KNAPSACK_DIR: ${CI_PROJECT_DIR}/qa/knapsack
    GIT_STRATEGY: none
  script:
    - echo "KNAPSACK_TEST_FILE_PATTERN is ${KNAPSACK_TEST_FILE_PATTERN}"
    # when using qa-image, code runs in /home/gitlab/qa folder
    - bundle exec rake "knapsack:download[test]"
    - '[ -n "$QA_TESTS" ] && bundle exec rake "knapsack:create_reports_for_selective"'
    - mkdir -p "$KNAPSACK_DIR" && cp knapsack/*.json "${KNAPSACK_DIR}/"
  allow_failure: true
  artifacts:
    paths:
      - qa/knapsack/*.json
    expire_in: 1 day

.download-fast-quarantine-report:
  image: ${GITLAB_DEPENDENCY_PROXY_ADDRESS}alpine:edge
  stage: .pre
  variables:
    GIT_STRATEGY: none
  before_script:
    - apk add --no-cache --update curl bash
  script:
    - mkdir -p "${QA_RSPEC_REPORT_PATH}"
    - |
      if [[ ! -f "${QA_RSPEC_REPORT_PATH}/${RSPEC_FAST_QUARANTINE_FILE}" ]]; then
        curl --location -o "${QA_RSPEC_REPORT_PATH}/${RSPEC_FAST_QUARANTINE_FILE}" "https://gitlab-org.gitlab.io/quality/engineering-productivity/fast-quarantine/${RSPEC_FAST_QUARANTINE_PATH}" ||
          echo "" > "${QA_RSPEC_REPORT_PATH}/${RSPEC_FAST_QUARANTINE_FILE}"
      fi
  allow_failure: true
  artifacts:
    paths:
      - "${QA_RSPEC_REPORT_PATH}/${RSPEC_FAST_QUARANTINE_FILE}"
    expire_in: 1 day

.upload-knapsack-report:
  extends:
    - .generate-knapsack-report-base
    - .qa-install
    - .ruby-image
  stage: report
  when: always

.export-test-metrics:
  extends:
    - .qa-install
    - .ruby-image
  stage: report
  when: always
  variables:
    QA_METRICS_REPORT_FILE_PATTERN: $CI_PROJECT_DIR/gitlab-qa-run-*/**/test-metrics-*.json
  script:
    - bundle exec rake "ci:export_test_metrics[$QA_METRICS_REPORT_FILE_PATTERN]"

.generate-test-session:
  extends:
    - .qa-install
    - .ruby-image
  stage: report
  when: always
  variables:
    QA_RSPEC_JSON_FILE_PATTERN: "${CI_PROJECT_DIR}/gitlab-qa-run-*/**/rspec-*.json"
  script:
    - |
      bundle exec generate-test-session \
        --input-files "${QA_RSPEC_JSON_FILE_PATTERN}" \
        --project "gitlab-org/quality/testcase-sessions" \
        --token "${QA_TEST_SESSION_TOKEN}" \
        --ci-project-token "${GENERATE_TEST_SESSION_READ_API_REPORTER_TOKEN}" \
        --issue-url-file report_issue_url.txt
  artifacts:
    when: always
    expire_in: 1d
    paths:
      - qa/report_issue_url.txt

.notify-slack:
  extends:
    - .notify-slack-qa
    - .qa-install
    - .ruby-image
  stage: notify
  variables:
    QA_RSPEC_XML_FILE_PATTERN: "${CI_PROJECT_DIR}/gitlab-qa-run-*/**/rspec-*.xml"
    SLACK_ICON_EMOJI: ci_failing
    STATUS_SYM: ☠️
    STATUS: failed
    TYPE: "($QA_RUN_TYPE) "
    ALLURE_JOB_NAME: $QA_RUN_TYPE
  when: always
  script:
    - |
      if [ "$SUITE_FAILED" != "true" ] && [ "$SUITE_RAN" == "true" ]; then
        echo "Test suite passed. Exiting..."
        exit 0
      fi
    - bundle exec prepare-stage-reports --input-files "${QA_RSPEC_XML_FILE_PATTERN}"
    - !reference [.notify-slack-qa, script]

.failure-videos:
  variables:
    USE_SELENOID: "true"
    QA_RECORD_VIDEO: "true"
    QA_REMOTE_GRID: "selenoid:4444"

# ==========================================
# Pre stage
# ==========================================
dont-interrupt-me:
  stage: .pre
  interruptible: false
  script:
    - echo "This jobs makes sure this pipeline won't be interrupted! See https://docs.gitlab.com/ee/ci/yaml/#interruptible."
  rules:
    - if: '$CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH && $CI_MERGE_REQUEST_IID == null'
      allow_failure: true
    - if: '$CI_MERGE_REQUEST_EVENT_TYPE == "merged_result" || $CI_MERGE_REQUEST_EVENT_TYPE == "detached"'
      when: manual
      allow_failure: true
